<?php

namespace model;

use common\Model;

class members extends Model
{

	private $DB_TABLE = "member";
    private $DB;
    function __construct()
    {
        $this->DB = parent::db();
    }
	//查找一条信息
	public function findData()
	{
		$where['id'] = $_GET['id'];
		return $this->DB->table($this->DB_TABLE)->where($where)->find();
	}

	//添加数据
	public function insertData()
	{
		if (empty($_POST['name']))
			return ['status' => 0, 'msg' => '请填写必要数据'];
		$data = [
			'name' => VarFilter($_POST['name']),
			'duties' => VarFilter($_POST['duties']),
			'photo' => VarFilter($_POST['photo']),
			'duty' => VarFilter($_POST['duty']),
			'sort' => VarFilter($_POST['sort']),
			'status' => $_POST['status'],
		];
		$re_key = $this->DB->table($this->DB_TABLE)->insert($data); //$this->error($this->DB->getError());
		if ($re_key) {
			//写入日志
			SqlLog("插入成员功能信息 ID：" . $re_key);

			return ['status' => 1, 'msg' => '添加成功'];
		} else {
			return ['status' => 0, 'msg' => '数据错误！添加失败'];
		}
	}
	//关联更新数据
	public function updateData()
	{
		if (empty($_POST['name']))
			return ['status' => 0, 'msg' => '请填写必要数据'];
		$where['id'] = $_POST['id'];
		$data = [
			'name' => VarFilter($_POST['name']),
			'duties' => VarFilter($_POST['duties']),
			'photo' => VarFilter($_POST['photo']),
			'duty' => VarFilter($_POST['duty']),
			'sort' => VarFilter($_POST['sort']),
			'status' => $_POST['status'],
		];

		$re_key = $this->DB->table($this->DB_TABLE)->Where($where)->Update($data);
		if ($re_key) {
			//写入日志
			SqlLog("修改成员功能信息 ID：" . $_POST['id']);

			return ['status' => 1, 'msg' => '修改成功'];
		} else {
			return ['status' => 0, 'msg' => '数据没有变化'];
		}
	}


	public function switchStatus()
	{
		$where['id'] = $_POST['id'];
		$data = [
			'status' => $_POST['status']
		];
		SqlLog("切换成员显示状态 ID：" . $_POST['id']);//写入日志
		return $this->DB->table($this->DB_TABLE)->where($where)->Update($data);
	}


	//删除一条数据
	public function deleteOneData()
	{
		$where['id'] = $_POST['id'];
		SqlLog("删除成员功能信息 ID：" . $_POST['id']);//写入日志
		return $this->DB->table($this->DB_TABLE)->where($where)->Delete();
	}


	//获取数据并分页
	public function jsonPageSelect()
	{
		$p = intval($_GET['page'] ?? 0) ?: 1;
		$num = intval($_GET['limit'] ?? 0) ?: 15;
		$page = ['num' => $num, 'p' => $p, 'return' => true];
		$result['data'] = $this->DB->table($this->DB_TABLE)->Page($page)->order('sort asc')->select();
		$result['page'] = $this->DB->getPage();
		return $result;
	}
}
